﻿#include "hzpch.h"
#include "ImGuiLayer.h"
#include "SourceCode/Base/Application.h"

#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"

#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <ImGuizmo.h>

namespace Hazal
{
	CImGuiLayer::CImGuiLayer()
		: CLayer("ImGuiLayer"), m_fTime(0.0f), m_bBlockEvents(true)
	{
	}

	CImGuiLayer::~CImGuiLayer()
	{
	}

	void CImGuiLayer::OnAttach()
	{
		IMGUI_CHECKVERSION();
		ImGui::CreateContext();
		ImGuiIO& io = ImGui::GetIO();
		io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
		io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
		io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;

		/// Add font from file
		float fFontSize = 25.0f;
		io.Fonts->AddFontFromFileTTF("../../Assets/Fonts/OpenSans/OpenSans-Bold.ttf", fFontSize);
		io.FontDefault = io.Fonts->AddFontFromFileTTF("../../Assets/Fonts/OpenSans/OpenSans-Regular.ttf", fFontSize);

		/// ImGui Style
		SetDarkThemeColors();
		
/*ImGui::StyleColorsDark();

		ImGuiStyle& style = ImGui::GetStyle();
		if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
		{
			style.WindowRounding = 0.0f;
			style.Colors[ImGuiCol_WindowBg].w = 1.0f;
		}*/


		/// Init renderer bindings
		CWindow& rWindow = CApplication::Instance().GetWindow();
		GLFWwindow* pWindow = static_cast<GLFWwindow*>(rWindow.GetNativeWindow());

		ImGui_ImplGlfw_InitForOpenGL(pWindow, true);
		ImGui_ImplOpenGL3_Init("#version 410");
	}

	void CImGuiLayer::OnDetach()
	{
		ImGui_ImplGlfw_Shutdown();
		ImGui_ImplOpenGL3_Shutdown();
		ImGui::DestroyContext();
	}

	void CImGuiLayer::OnEvent(CEvent& event)
	{
		if (m_bBlockEvents)
		{
			CEventDispatcher dispatcher(event);

			dispatcher.Dispatch<CMouseMovedEvent>(HZ_BIND_EVENT_FN(CImGuiLayer::OnImGuiCaptureMouseMoved));
			dispatcher.Dispatch<CMouseScrolledEvent>(HZ_BIND_EVENT_FN(CImGuiLayer::OnImGuiCaptureMouseScrolled));
			dispatcher.Dispatch<CKeyPressedEvent>(HZ_BIND_EVENT_FN(CImGuiLayer::OnImGuiCaptureKeyboard));
		}
	}

	void CImGuiLayer::Begin()
	{
		ImGui_ImplGlfw_NewFrame();
		ImGui_ImplOpenGL3_NewFrame();
		ImGui::NewFrame();

		/// ImGuizmo begin
		ImGuizmo::BeginFrame();
	}
	void CImGuiLayer::End()
	{
		ImGuiIO& io = ImGui::GetIO();
		CWindow& rWindow = CApplication::Instance().GetWindow();
		io.DisplaySize = ImVec2(rWindow.GetWidth(), rWindow.GetHeight());

		/// Rendering
		ImGui::Render();
		ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

		if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
		{
			GLFWwindow* pCurContext = glfwGetCurrentContext();
			ImGui::UpdatePlatformWindows();
			ImGui::RenderPlatformWindowsDefault();

			glfwMakeContextCurrent(pCurContext);
		}
	}
	bool CImGuiLayer::OnImGuiCaptureMouseMoved(CMouseMovedEvent& event)
	{
		bool bRet = ImGui::GetIO().WantCaptureMouse;
		return bRet;
	}

	bool CImGuiLayer::OnImGuiCaptureKeyboard(CKeyPressedEvent& event)
	{
		bool bRet = ImGui::GetIO().WantCaptureKeyboard;
		return bRet;
	}
	bool CImGuiLayer::OnImGuiCaptureMouseScrolled(CMouseScrolledEvent& event)
	{
		bool bRet = ImGui::GetIO().WantCaptureMouse;
		return bRet;
	}

	void CImGuiLayer::SetDarkThemeColors()
	{
		auto& colors = ImGui::GetStyle().Colors;
		colors[ImGuiCol_WindowBg] = { 0.1f, 0.105f, 0.11f, 1.0f };

		/// Headers
		colors[ImGuiCol_Header] = { 0.2f, 0.205f, 0.21f, 1.0f };
		colors[ImGuiCol_HeaderHovered] = { 0.3f, 0.305f, 0.31f, 1.0f };
		colors[ImGuiCol_HeaderActive] = { 0.15f, 0.1505f, 0.151f, 1.0f };

		/// Buttons
		colors[ImGuiCol_Button] = { 0.2f, 0.205f, 0.21f, 1.0f };
		colors[ImGuiCol_ButtonHovered] = { 0.3f, 0.305f, 0.31f, 1.0f };
		colors[ImGuiCol_ButtonActive] = { 0.15f, 0.1505f, 0.151f, 1.0f };

		/// Frame background
		colors[ImGuiCol_FrameBg] = { 0.2f, 0.205f, 0.21f, 1.0f };
		colors[ImGuiCol_FrameBgHovered] = { 0.3f, 0.305f, 0.31f, 1.0f };
		colors[ImGuiCol_FrameBgActive] = { 0.15f, 0.1505f, 0.151f, 1.0f };

		/// Tabs
		colors[ImGuiCol_Tab] = { 0.15f, 0.1505f, 0.151f, 1.0f };
		colors[ImGuiCol_TabHovered] = { 0.38f, 0.3805f, 0.381f, 1.0f };
		colors[ImGuiCol_TabActive] = { 0.28f, 0.2805f, 0.281f, 1.0f };
		colors[ImGuiCol_TabUnfocused] = { 0.15, 0.1505f, 0.151f, 1.0f };
		colors[ImGuiCol_TabUnfocusedActive] = { 0.2, 0.205f, 0.21f, 1.0f };

		/// Title
		colors[ImGuiCol_TitleBg] = { 0.15f, 0.1505f, 0.151f, 1.0f };
		colors[ImGuiCol_TitleBgActive] = { 0.15f, 0.1505f, 0.151f, 1.0f };
		colors[ImGuiCol_TitleBgCollapsed] = { 0.95f, 0.1505f, 0.951f, 1.0f };

	}
}